﻿using UnityEngine;
using System.Collections.Generic;
using System;

public class User
{
	/// <summary>
	/// The instance.
	/// </summary>
	private static User instance;
	public static User Instance
	{
		get 
		{
			if (instance == null)
			{
				instance = new User();
			}
			
			return instance;
		}
	}

	/// <summary>
	/// The unique ID.
	/// </summary>
	private Guid uniqueID;
	public Guid UniqueID
	{
		get { return uniqueID; }
	}

	/// <summary>
	/// The ongoing matches.
	/// </summary>
	private List<Match> ongoingMatches;
	public List<Match> Matches
	{
		get { return ongoingMatches; }
	}

	/// <summary>
	/// The nick.
	/// </summary>
	private string nick;
	public string Nick { get { return this.nick; } }

	/// <summary>
	/// The facebook I.
	/// </summary>
	private string facebookID;
	public string FacebookID { get { return this.facebookID; } }

	/// <summary>
	/// Initializes a new instance of the <see cref="User"/> class.
	/// </summary>
	private User()
	{
		this.nick = string.Empty;
		this.facebookID = string.Empty;

		uniqueID = new Guid();

		ongoingMatches = new List<Match>();
	}

	/// <summary>
	/// Sets the user facebook.
	/// </summary>
	/// <param name="facebookID">Facebook ID.</param>
	/// <param name="facebookName">Facebook name.</param>
	public void SetUserFacebook(string facebookID, string facebookName)
	{
		this.nick = facebookName;
		this.facebookID = facebookID;
		
		if (string.IsNullOrEmpty(PlayerPrefs.GetString("UniqueID")))
		{
			uniqueID = Guid.NewGuid();
		}
		else
		{
			uniqueID = new Guid(PlayerPrefs.GetString("UniqueID"));
		}

		PlayerPrefs.SetString("UserName", this.nick);
		PlayerPrefs.SetString("FacebookID", this.facebookID);
		PlayerPrefs.SetString("UniqueID", uniqueID.ToString());
	}

	/// <summary>
	/// Sets the user normal.
	/// </summary>
	/// <param name="nickName">Nick name.</param>
	public void SetUserNormal(string nickName)
	{
		this.nick = nickName;
		this.facebookID = string.Empty;

		if (string.IsNullOrEmpty(PlayerPrefs.GetString("UniqueID")))
		{
			uniqueID = Guid.NewGuid();
		}
		else
		{
			uniqueID = new Guid(PlayerPrefs.GetString("UniqueID"));
		}

		Debug.Log (this.nick);
		Debug.Log (this.facebookID);
		Debug.Log (uniqueID);
		
		PlayerPrefs.SetString("UserName", this.nick);
		PlayerPrefs.SetString("FacebookID", this.facebookID);
		PlayerPrefs.SetString("UniqueID", uniqueID.ToString());
	}

	/// <summary>
	/// Adds the match.
	/// </summary>
	/// <param name="match">Match.</param>
	public void AddMatch(Match match)
	{
		ongoingMatches.Add(match);
	}
}
